package com.me.regex;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.me.util.AppConstant;
import com.me.util.AppUtil;

public class AnalyseTitle {
	
	final static String PATTERN_KEYWORD = "(title|theme|topic)[:\\s\n\n\b]+([^\n\r]+)";
	final static String TITLE_PATTERN = "(breakfast|lunch|dinner|meeting|seminar)";

	public static ArrayList<String> analyse(String input) {

		ArrayList<String> output = new ArrayList<String>();
				
		Pattern patternWithKeyword = Pattern.compile(PATTERN_KEYWORD, Pattern.CASE_INSENSITIVE);
		
		Matcher matcherWithKeyword = patternWithKeyword.matcher(input);
		
		while (matcherWithKeyword.find()) {
			output.add(matcherWithKeyword.group(2));
		}

		if (output.size() > 0) 				
			return output;					//return if keyword exists in the input string
		
		
		String[] tokens = input.split("\\b");
		ArrayList<String> words = AppUtil.App.dataService.searchTitles(tokens);
	
		
		if (words.size() > 0) {

			String titlePattern = Functions.buildPattern(words);
									
			Pattern pattern = Pattern.compile(titlePattern, Pattern.CASE_INSENSITIVE);
		
			Matcher matcher = pattern.matcher(input);
			
			while (matcher.find()) {			
				output.add(matcher.group());
			}
		}
		
		Pattern pattern = Pattern.compile(TITLE_PATTERN, Pattern.CASE_INSENSITIVE);
		
		Matcher matcher = pattern.matcher(input);
		
		while (matcher.find()) {			
			output.add(matcher.group());
		}

		
		if (output.size() == 0)
			output.add("");			//empty string
		
		return output;
	}

}
